package com.example.anpnovel.controller.crud;

import com.example.anpapi.entity.po.novelmodel.Favorite.Favorite;
import com.example.anpapi.entity.vo.R;
import com.example.anpnovel.service.interfaces.FavoriteService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/favorite")
public class FavoriteController {

    @Autowired
    private FavoriteService favoriteService;

    /**
     * 收藏添加
     * @param favorite
     * @return
     */
    @PostMapping("/save")
    public R<Favorite> save(@RequestBody Favorite favorite){
        log.info("收藏添加:{}", favorite);
        favoriteService.save(favorite);
        return R.success(favorite,"收藏添加成功");
    }

    /**
     * 根据收藏id删除收藏
     * @param favorite
     * @return
     */
    @DeleteMapping("/remove")
    public R<String> remove(@RequestBody Favorite favorite){
        log.info("收藏删除:{}", favorite);
        if(favoriteService.getById(favorite.getId()) != null)
        {
            favoriteService.removeById(favorite.getId());
            return R.success(null,"收藏删除成功");
        }
        return R.error("未找到该收藏");
    }

    /**
     * 根据收藏id查找收藏
     * @param id
     * @return
     */
    @GetMapping("/get")
    public R<Favorite> get(@RequestParam(value = "id") Long id) {
        log.info("查询收藏的id为{}", id);
        Favorite favorite = favoriteService.getById(id);
        return R.success(favorite, "查询收藏成功");
    }

    /**
     * 根据用户id和作品id查找收藏
     * @param userId
     * @param novelId
     * @return
     */
    @GetMapping("/getByUserAndNovel")
    public R<Favorite> getByUserAndNovel(@RequestParam(value = "userId") Long userId, 
                                          @RequestParam(value = "novelId") Long novelId) {
        log.info("查询收藏 - userId:{}, novelId:{}", userId, novelId);
        Favorite favorite = favoriteService.lambdaQuery()
                .eq(Favorite::getUserId, userId)
                .eq(Favorite::getNovelId, novelId)
                .one();
        return R.success(favorite, "查询收藏成功");
    }

    /**
     * 根据用户id查找所有收藏
     * @param userId
     * @return
     */
    @GetMapping("/getByUserId")
    public R<List<Favorite>> getByUserId(@RequestParam(value = "userId") Long userId) {
        log.info("查询用户{}的所有收藏", userId);
        List<Favorite> favorites = favoriteService.lambdaQuery()
                .eq(Favorite::getUserId, userId)
                .orderByDesc(Favorite::getCreatedTime)
                .list();
        return R.success(favorites, "查询收藏成功");
    }

    /**
     * 根据作品id查找所有收藏
     * @param novelId
     * @return
     */
    @GetMapping("/getByNovelId")
    public R<List<Favorite>> getByNovelId(@RequestParam(value = "novelId") Long novelId) {
        log.info("查询作品{}的所有收藏", novelId);
        List<Favorite> favorites = favoriteService.lambdaQuery()
                .eq(Favorite::getNovelId, novelId)
                .orderByDesc(Favorite::getCreatedTime)
                .list();
        return R.success(favorites, "查询收藏成功");
    }

    /**
     * 修改收藏
     * @param favorite
     * @return
     */
    @PostMapping("/update")
    public R<Favorite> update(@RequestBody Favorite favorite){
        log.info("修改的收藏为{}", favorite);
        favoriteService.updateById(favorite);
        return R.success(favorite,"修改收藏成功");
    }

}

